package org.custom.spring.boot.datasource.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpHeaders;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * RestTemplate相关属性配置
 *
 * @author 李坤
 * @date 2022/3/31 17:21
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("待解析网络接口实体类")
public class RestTemplatePropertity implements Serializable {
    /**
     * 请求地址
     */
    @NotEmpty(message = "url不能为空")
    @Pattern(regexp = "^((https|http|ftp|rtsp|mms)?://)[^\\s]+", message = "url格式不正确")
    @ApiModelProperty(value = "url", notes = "请求地址")
    private String url;

    /**
     * 请求参数
     */
    @ApiModelProperty(value = "params", notes = "请求参数")
    private Object params;

    /**
     * 请求方式
     */
    @ApiModelProperty(value = "httpMethod", notes = "请求方式")
    private String httpMethod;

    /**
     * 请求变量集合
     */
    @ApiModelProperty(value = "uriVariables", notes = "请求变量集合")
    private List<String> uriVariables;

    /**
     * 自定义请求头信息
     */
    @ApiModelProperty(value = "requestHeaders", notes = "自定义请求头信息")
    private Map<String, String> requestHeaders;

    /**
     * 获取请求头信息
     *
     * @return 请求头信息
     */
    public HttpHeaders obtainHttpHeaders() {
        HttpHeaders httpHeaders = new HttpHeaders();
        // 如果自定义请求头信息存在，则封装自定义请求头信息
        Optional.ofNullable(requestHeaders).ifPresent(headers -> headers.forEach(httpHeaders::add));

        return httpHeaders;
    }
}
